package com.fuda.trade.ui.strategies

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.fuda.trade.model.TradeStrategyData
import com.fuda.trade.R
import com.fuda.trade.model.TradeStrategy

class StrategiesAdapter: ListAdapter<TradeStrategy, StrategiesAdapter.StrategyViewHolder>(StrategyDiffCallback()) {

    companion object {
        val TAG: String = StrategiesAdapter::class.java.simpleName
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StrategyViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val itemView = when (viewType) {
            TradeStrategyData.Short::class.hashCode() -> inflater.inflate(R.layout.item_strategy_short, parent, false)
            TradeStrategyData.Long::class.hashCode() -> inflater.inflate(R.layout.item_strategy_long, parent, false)
            else -> throw IllegalArgumentException("Invalid view type")
        } 
        return StrategyViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: StrategyViewHolder, position: Int) {
        val strategy = getItem(position)
        holder.bind(strategy)
    }


    override fun getItemViewType(position: Int): Int {
        val tradeStrategy: TradeStrategy = getItem(position)
        return when (tradeStrategy.data) {
            is TradeStrategyData.Short -> TradeStrategyData.Short::class.hashCode()
            is TradeStrategyData.Long -> TradeStrategyData.Long::class.hashCode()
            else -> throw IllegalArgumentException("Unknown data type")
        }
    }

    inner class StrategyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private lateinit var description: TextView
        private lateinit var radioGroup: RadioGroup
        private lateinit var radioAuto: RadioButton
        private lateinit var radioManual: RadioButton

        fun bind(tradeOrder: TradeStrategy) {
            description = itemView.findViewById(R.id.description)
            description.text = TradeStrategy.serialize(tradeOrder)

            radioGroup = itemView.findViewById(R.id.radio_group)
            radioAuto = itemView.findViewById(R.id.radio_auto)
            radioManual = itemView.findViewById(R.id.radio_manual)
            val isRadioAuto =radioAuto.isChecked
            radioAuto.setOnClickListener {
                // Manually select the RadioButton
                if (isRadioAuto){
                    radioGroup.check(R.id.radio_auto)
                }else{
                    radioGroup.check(R.id.radio_manual)
                }
            }
            radioManual.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    // Manually select the RadioButton
                    if (isRadioAuto){
                        radioGroup.check(R.id.radio_auto)
                    }else{
                        radioGroup.check(R.id.radio_manual)
                    }
                }
            })
        }
    }



    class StrategyDiffCallback : DiffUtil.ItemCallback<TradeStrategy>() {
        override fun areItemsTheSame(oldItem: TradeStrategy, newItem: TradeStrategy): Boolean {
            return oldItem::class == newItem::class && oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: TradeStrategy, newItem: TradeStrategy): Boolean {
            return oldItem == newItem
        }
    }
}